package com.ymatou.yid.testcase;

import java.util.UUID;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.yid.parameter.YidBean;
import com.ymatou.yid.service.YidCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.utils.TestDataManager;

/**
 * 用户标记统一ID接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_Yid {
	private static YidBean yb;
	private static YidCall yc;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("用户标记统一ID接口");
	}

	@Before
	public void caseUp() {
		yb = new YidBean();
		yc = new YidCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(yc.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_Yid_001() {
		Logger.start(true, "调用用户标记统一ID成功");
		try {
			int uid = Integer.valueOf(TestDataManager.getRandomNum(7));
			String cid = TestDataManager.getRandomUUID().toString();
			String did = TestDataManager.getRandomUUID().toString();
			Logger.comment("绑定UUID信息");
			yb.setUserid(uid);
			yb.setDeviceid(did);
			yb.setCookieid(cid);
			yc.setData(yb);
			yc.callService();
			Logger.verifyNotNull(yc.getReturnData(), "验证有返回UUID");
			Logger.verifyNotNull(UUID.fromString(yc.getReturnData()),"验证UUID格式");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_Yid_002() {
		Logger.start(true, "调用用户标记统一ID成功后只使用userid查询用户标记成功");
		try {
			int uid = Integer.valueOf(TestDataManager.getRandomNum(7));
			String cid = TestDataManager.getRandomUUID().toString();
			String did = TestDataManager.getRandomUUID().toString();
			Logger.comment("绑定UUID信息");
			yb.setUserid(uid);
			yb.setDeviceid(did);
			yb.setCookieid(cid);
			yc.setData(yb);
			yc.callService();

			Logger.verifyNotNull(yc.getReturnData(), "验证有返回UUID");
			String fyid = yc.getReturnData();
			Logger.comment("使用userid查询");
			yb = new YidBean();
			yc = new YidCall();
			yb.setUserid(uid);
			yc.setData(yb);
			yc.callService();
			Logger.comment("第二次调用用户标记统一ID send:" + yc.getEntityString());
			Logger.comment("第二次调用用户标记统一ID response:" + yc.getReturnData());
			Logger.verifyEquals(fyid, yc.getReturnData(), "验证返回UUID一致");
			Logger.verifyNotNull(UUID.fromString(yc.getReturnData()),"验证UUID格式");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_Yid_003() {
		Logger.start(true, "调用用户标记统一ID成功后只使用cookieid查询用户标记成功");
		try {
			int uid = Integer.valueOf(TestDataManager.getRandomNum(7));
			String cid = TestDataManager.getRandomUUID().toString();
			String did = TestDataManager.getRandomUUID().toString();
			Logger.comment("绑定UUID信息");
			yb.setUserid(uid);
			yb.setDeviceid(did);
			yb.setCookieid(cid);
			yc.setData(yb);
			yc.callService();

			Logger.verifyNotNull(yc.getReturnData(), "验证有返回UUID");
			String fyid = yc.getReturnData();
			Logger.comment("使用cookieid查询");
			yb = new YidBean();
			yc = new YidCall();
			yb.setCookieid(cid);
			yc.setData(yb);
			yc.callService();
			Logger.comment("第二次调用用户标记统一ID send:" + yc.getEntityString());
			Logger.comment("第二次调用用户标记统一ID response:" + yc.getReturnData());
			Logger.verifyEquals(fyid, yc.getReturnData(), "验证返回UUID一致");
			Logger.verifyNotNull(UUID.fromString(yc.getReturnData()),"验证UUID格式");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_Yid_004() {
		Logger.start(true, "调用用户标记统一ID成功后只使用deviceid查询用户标记成功");
		try {
			int uid = Integer.valueOf(TestDataManager.getRandomNum(7));
			String cid = TestDataManager.getRandomUUID().toString();
			String did = TestDataManager.getRandomUUID().toString();
			Logger.comment("绑定UUID信息");
			yb.setUserid(uid);
			yb.setDeviceid(did);
			yb.setCookieid(cid);
			yc.setData(yb);
			yc.callService();

			Logger.verifyNotNull(yc.getReturnData(), "验证有返回UUID");
			Logger.comment("使用deviceid查询");
			String fyid = yc.getReturnData();
			yb = new YidBean();
			yc = new YidCall();
			yb.setDeviceid(did);
			yc.setData(yb);
			yc.callService();
			Logger.comment("第二次调用用户标记统一ID send:" + yc.getEntityString());
			Logger.comment("第二次调用用户标记统一ID response:" + yc.getReturnData());
			Logger.verifyEquals(fyid, yc.getReturnData(), "验证返回UUID一致");
			Logger.verifyNotNull(UUID.fromString(yc.getReturnData()),"验证UUID格式");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_Yid_005() {
		Logger.start(true, "userid绑定多个cookieid后每个cookieid都能查询出正确的UUID");
		try {
			int uid = Integer.valueOf(TestDataManager.getRandomNum(7));
			String cid = TestDataManager.getRandomUUID().toString();
			String cid2 = TestDataManager.getRandomUUID().toString();
			Logger.comment("cookieid绑定UUID信息");
			yb.setUserid(uid);
			yb.setCookieid(cid);
			yc.setData(yb);
			yc.callService();

			Logger.verifyNotNull(yc.getReturnData(), "验证有返回UUID");
			String fyid = yc.getReturnData();
			Logger.comment("cookieid2绑定UUID信息");
			yb = new YidBean();
			yc = new YidCall();
			yb.setUserid(uid);
			yb.setCookieid(cid2);
			yc.setData(yb);
			yc.callService();
			Logger.comment("第二次调用用户标记统一ID send:" + yc.getEntityString());
			Logger.comment("第二次调用用户标记统一ID response:" + yc.getReturnData());
			Logger.verifyEquals(fyid, yc.getReturnData(), "验证返回UUID一致");

			yb = new YidBean();
			yc = new YidCall();
			yb.setCookieid(cid);
			yc.setData(yb);
			yc.callService();
			Logger.verifyEquals(fyid, yc.getReturnData(), "验证第一个cookieid返回UUID一致");
			yb = new YidBean();
			yc = new YidCall();
			yb.setCookieid(cid2);
			yc.setData(yb);
			yc.callService();
			Logger.verifyEquals(fyid, yc.getReturnData(), "验证第二个cookieid返回UUID一致");
			Logger.verifyNotNull(UUID.fromString(yc.getReturnData()),"验证UUID格式");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_Yid_006() {
		Logger.start(true, "userid绑定多个deviceid后每个deviceid都能查询出正确的UUID");
		try {
			int uid = Integer.valueOf(TestDataManager.getRandomNum(7));
			String did = TestDataManager.getRandomUUID().toString();
			String did2 = TestDataManager.getRandomUUID().toString();
			Logger.comment("deviceid绑定UUID信息");
			yb.setUserid(uid);
			yb.setCookieid(did);
			yc.setData(yb);
			yc.callService();

			Logger.verifyNotNull(yc.getReturnData(), "验证有返回UUID");
			String fyid = yc.getReturnData();
			Logger.comment("deviceid2绑定UUID信息");
			yb = new YidBean();
			yc = new YidCall();
			yb.setUserid(uid);
			yb.setCookieid(did2);
			yc.setData(yb);
			yc.callService();
			Logger.comment("第二次调用用户标记统一ID send:" + yc.getEntityString());
			Logger.comment("第二次调用用户标记统一ID response:" + yc.getReturnData());
			Logger.verifyEquals(fyid, yc.getReturnData(), "验证返回UUID一致");

			yb = new YidBean();
			yc = new YidCall();
			yb.setCookieid(did);
			yc.setData(yb);
			yc.callService();
			Logger.verifyEquals(fyid, yc.getReturnData(), "验证第一个cookieid返回UUID一致");
			yb = new YidBean();
			yc = new YidCall();
			yb.setCookieid(did2);
			yc.setData(yb);
			yc.callService();
			Logger.verifyEquals(fyid, yc.getReturnData(), "验证第二个cookieid返回UUID一致");
			Logger.verifyNotNull(UUID.fromString(yc.getReturnData()),"验证UUID格式");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_Yid_007() {
		Logger.start(true, "cookieid绑定多个userid后cookieid查询出最后一次绑定的userid对应的UUID");
		try {
			int uid = Integer.valueOf(TestDataManager.getRandomNum(7));
			int uid2 = Integer.valueOf(TestDataManager.getRandomNum(7));
			String cid = TestDataManager.getRandomUUID().toString();
			Logger.comment("绑定UUID信息");
			yb.setUserid(uid);
			yb.setCookieid(cid);
			yc.setData(yb);
			yc.callService();

			yb = new YidBean();
			yc = new YidCall();
			yb.setUserid(uid2);
			yb.setCookieid(cid);
			yc.setData(yb);
			yc.callService();

			String fyid = yc.getReturnData();
			yb = new YidBean();
			yc = new YidCall();
			yb.setCookieid(cid);
			yc.setData(yb);
			yc.callService();

			Logger.verifyEquals(fyid, yc.getReturnData(), "验证第二个cookieid返回UUID一致");
			Logger.verifyNotNull(UUID.fromString(yc.getReturnData()),"验证UUID格式");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_Yid_008() {
		Logger.start(true, "deviceid绑定多个userid后deviceid查询出最后一次绑定的userid对应的UUID");
		try {
			int uid = Integer.valueOf(TestDataManager.getRandomNum(7));
			int uid2 = Integer.valueOf(TestDataManager.getRandomNum(7));
			String did = TestDataManager.getRandomUUID().toString();
			Logger.comment("绑定UUID信息");
			yb.setUserid(uid);
			yb.setDeviceid(did);
			yc.setData(yb);
			yc.callService();

			yb = new YidBean();
			yc = new YidCall();
			yb.setUserid(uid2);
			yb.setDeviceid(did);
			yc.setData(yb);
			yc.callService();

			String fyid = yc.getReturnData();
			yb = new YidBean();
			yc = new YidCall();
			yb.setDeviceid(did);
			yc.setData(yb);
			yc.callService();

			Logger.verifyEquals(fyid, yc.getReturnData(), "验证第二个deviceid返回UUID一致");
			Logger.verifyNotNull(UUID.fromString(yc.getReturnData()),"验证UUID格式");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_Yid_009() {
		Logger.start(true, "deviceid和cookieid绑定不同userid后用cookieid和deviceid联合查询,对应的UUID以deviceid为准");
		try {
			int uid = Integer.valueOf(TestDataManager.getRandomNum(7));
			int uid2 = Integer.valueOf(TestDataManager.getRandomNum(7));
			String did = TestDataManager.getRandomUUID().toString();
			String cid = TestDataManager.getRandomUUID().toString();

			Logger.comment("deviceid绑定UUID信息");
			yb = new YidBean();
			yc = new YidCall();
			yb.setUserid(uid2);
			yb.setDeviceid(did);
			yc.setData(yb);
			yc.callService();

			String fyid = yc.getReturnData();

			Logger.comment("cookieid绑定UUID信息");
			yb = new YidBean();
			yc = new YidCall();
			yb.setUserid(uid);
			yb.setCookieid(cid);
			yc.setData(yb);
			yc.callService();

			yb = new YidBean();
			yc = new YidCall();
			yb.setCookieid(cid);
			yb.setDeviceid(did);
			yc.setData(yb);
			yc.callService();

			Logger.verifyEquals(fyid, yc.getReturnData(), "验证查询结果UUID和deviceid绑定一致");
			Logger.verifyNotNull(UUID.fromString(yc.getReturnData()),"验证UUID格式");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}